<template>
  <div class="relative w-full h-full overflow-y-auto">
    <PageHeader :title="$t('service.detail.退款详情')">
      <template #right>
        <el-button type="primary" @click="goBack" size="small">
          <SvgIcon name="iconfanhui" color="#fff" class="mr4" />{{ $t('service.detail.返回上级页面') }}
        </el-button>
      </template>
    </PageHeader>

    <Step :detail="detailInfo" v-show="detailInfo.id"/>

    <template v-if="detailInfo.refundAskType == 2 && detailInfo.refundCheckTime">
      <div class="mt15 mb15 pl5 color-333333 font-700">{{ $t('service.detail.买家退货信息') }}:</div>
      <el-table
        :show-header="false"
        :data="sendInfo"
        style="width: 100%"
        :cell-class-name="cellClassName"
        border
        :span-method="arraySpanMethod"
      >
        <el-table-column prop="label1" />
        <el-table-column prop="value1" />
        <el-table-column prop="label2" />
        <el-table-column prop="value2" />
      </el-table>
    </template>

    <div class="mt15 mb15 pl5 color-333333 font-700">{{ $t('service.detail.退款信息') }}:</div>
    <el-table
      :show-header="false"
      :data="tableData"
      style="width: 100%"
      :cell-class-name="cellClassName"
      border
      :span-method="arraySpanMethod"
    >
      <el-table-column prop="label1" />
      <el-table-column prop="value1" />
      <el-table-column prop="label2" />
      <el-table-column prop="value2" />
    </el-table>

    <div class="mt15 mb15 pl5 color-333333 font-700">{{ $t('service.detail.退款凭证信息') }}:</div>
    <el-table
      :show-header="false"
      :data="tableData2"
      style="width: 100%"
      :cell-class-name="cellClassName"
      border
      :span-method="arraySpanMethod2"
    >
      <el-table-column prop="label1" />
      <el-table-column prop="value1">
        <template #default="scope">
          <div class="flex_aic" v-if="scope.row.value1">
            <el-image
                v-for="(img, index) in scope.row.value1.split(',')"
                :key="img"
                style="width: 100px; height: 100px"
                :src="img"
                :zoom-rate="1.2"
                :max-scale="7"
                :min-scale="0.2"
                :preview-src-list="scope.row.value1.split(',')"
                show-progress
                :initial-index="4"
                fit="cover"
                preview-teleported
            />
          </div>
          <span v-else>{{ $t('service.detail.暂无凭证') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="label2" />
      <el-table-column prop="value2" />
    </el-table>

    <Kaipiao :orderDetail="detailInfo" />

    <div class="mt15 mb15 pl5 color-333333 font-700">{{ $t('service.detail.商品信息') }}:</div>
    <el-table
      :data="detailInfo.saleSkus"
      style="width: 100%"
      border
      class="cover-table"
    >
      <el-table-column :label="$t('service.detail.序号')" width="100" type="index" />
      <el-table-column prop="info" :label="$t('service.detail.商品信息')">
        <template #default="{ row }">
          <div class="flex_aic">
            <el-image
              style="width: 100px; height: 100px"
              :src="row.skuSpecDetailImage"
              :zoom-rate="1.2"
              :max-scale="7"
              :min-scale="0.2"
              :preview-src-list="[row.skuSpecDetailImage]"
              show-progress
              :initial-index="4"
              fit="cover"
              preview-teleported
              class="flex-shrink-0"
            />
            <div class="ml10 w-380 text-left">
              <div>{{ row["skuName" + locale] }}</div>
              <div>
                {{ row["skuClassify1Name" + locale] }} >
                {{ row["skuClassify2Name" + locale] }}
              </div>
              <div class="flex_aic">
                {{ $t('service.table.规格') }}：
                <span class="goods_spec flex_aic gap-4 flex-wrap">
                  <span v-for="spec in row.skuSpecDetail" :key="spec.specId"
                    >{{ spec["specName" + locale] }}-{{
                      spec["specDetailName" + locale]
                    }}</span
                  >
                </span>
              </div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="skuStockPrice" :label="$t('service.detail.商品单价')" width="160">
        <template #default="{ row }">
          <span>￥{{ row.skuStockPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="refundSkuCount" :label="$t('service.detail.退款数量')" width="160" />
      <el-table-column
        prop="skuPostage"
        :label="$t('service.detail.商品运费')"
        width="160"
        :formatter="(row) => '￥' + row.skuPostage"
      />
      <el-table-column prop="mergeColumn" :label="$t('orderTable.抵扣')" width="300">
        <template #default="{ row }">
          <div class="border-left-right">
            <div>
              <span class="color-999999">{{ $t('orderTable.优惠券抵扣金额') }}：</span
              >{{ row.payDiscountCoupon }}
            </div>
            <div>
              <span class="color-999999">{{ $t('orderTable.积分抵扣金额') }}：</span
              >{{ row.payDiscountPoint }}
            </div>
            <div>
              <span class="color-999999">{{ $t('orderTable.活动抵扣金额') }}：</span
              >{{ row.payDiscountActivity }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="payRealSku"
        :label="$t('service.detail.实付金额') + '('+$t('service.detail.不含运费')+')'"
        width="160"
        :formatter="(row) => '￥' + row.payRealSku"
      />
      
      <!-- <el-table-column prop="refundRealSku" label="退款金额(元)" width="160">
        <template #default="{ row }">
          <span>￥{{ row.refundRealSku || "--" }}</span>
        </template>
      </el-table-column> -->
    </el-table>

  </div>
</template>
<script setup>
import { ref, onMounted, watchEffect } from "vue";
import { useRouter } from "vue-router";
import Api from "@/config/api/api";
import { useRoute } from "vue-router";
import { useI18n } from "vue-i18n";
import { refundTypeMap } from "../data";
import Step from "./step.vue";
import moment from "moment";
import Kaipiao from "../../order_lists_detail/components/kaipiao.vue";

const { locale, t: $t } = useI18n();
const route = useRoute();
const router = useRouter();
const detailInfo = ref({});

// 退款信息
const tableData = ref([]);
// 退款凭证信息
const tableData2 = ref([]);
// 买家发货信息
const sendInfo = ref([]);
watchEffect(() => {
  tableData.value = [
    {
      label1: $t('service.detail.订单号'),
      value1: detailInfo.value.sourceSaleNo,
      label2: $t('service.detail.退款编号'),
      value2: detailInfo.value.saleNo,
    },
    {
      label1: $t('service.detail.退款金额') + "(元)",
      value1: detailInfo.value.refundReal,
      label2: $t('service.detail.退款原因'),
      value2: detailInfo.value["refundAskReasonName" + locale.value],
    },
    {
      label1: $t('service.detail.退款类型'),
      value1: $t('service.status.'+refundTypeMap[detailInfo.value.refundAskType]),
      label2: $t('service.detail.会员'),
      value2: detailInfo.value.vipNickName,
    },
    {
      label1: $t('service.detail.补充说明'),
      value1: detailInfo.value.refundAskRemark,
    },
  ];

  tableData2.value = [
    {
      label1: $t('service.detail.退款凭证'),
      value1: detailInfo.value.refundAskSkuImage,
    },
  ];
  // 买家退货信息
  sendInfo.value = [
    {
      label1: $t('service.detail.快递单号'),
      value1: detailInfo.value.refundVipSendNo,
      label2: $t('service.detail.发货时间'),
      value2: detailInfo.value.refundVipSendTime
          ? moment(detailInfo.value.refundVipSendTime).format(
              "YYYY-MM-DD HH:mm:ss"
          )
          : "",
    },
    {
      label1: $t('service.detail.退货地址'),
      value1:
          detailInfo.value["refundCheckAddressProvince" + locale.value] +
          detailInfo.value["refundCheckAddressCity" + locale.value] +
          detailInfo.value["refundCheckAddressArea" + locale.value] +
          detailInfo.value["refundCheckAddressAddress" + locale.value],
      label2: $t('service.detail.收货人'),
      value2: detailInfo.value["refundCheckAddressName" + locale.value],
    },
    {
      label1: $t('service.detail.联系电话'),
      value1: detailInfo.value.refundCheckAddressPhone,
    },
  ];

});


const cellClassName = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 0) {
    return "label-cell";
  }
  return "";
};

const arraySpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (rowIndex === 3 && columnIndex === 2) {
    return [1, 2];
  }
};

const arraySpanMethod2 = ({ row, column, rowIndex, columnIndex }) => {
  if (rowIndex === 0 && columnIndex === 2) {
    return [1, 2];
  }
};

const goBack = () => {
  router.back();
};

onMounted(async () => {
  const res = await Api.getOrderDetail(route.query.id);
  if (res.status === 200) {
    detailInfo.value = res.data || {};
  }
});
</script>

<style lang="scss" scoped>
.label-cell {
  background-color: #f5f5f5;
  font-weight: 500;
}
</style>
